package com.wmuj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wmuj.mapper.ClazzStudentMapper;
import com.wmuj.mapper.StudentMapper;
import com.wmuj.pojo.PageResult;
import com.wmuj.pojo.Student;
import com.wmuj.pojo.StudentQueryParam;
import com.wmuj.service.ClazzStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzStudentServiceImpl implements ClazzStudentService {
  /**
   * 学员管理分页查询+条件查询
   *
   * @param studentQueryParam
   * @return
   */
  @Autowired private StudentMapper studentMapper;

  @Override
  public PageResult<Student> queryStudentsList(StudentQueryParam studentQueryParam) {
    PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
    List<Student> rows = studentMapper.queryStudentsList(studentQueryParam);
    Page<Student> p = (Page<Student>) rows;
    return new PageResult<Student>(p.getTotal(), p.getResult());
  }

  /**
   * 根据路径参数传递过来的数组ids进行删除
   *
   * @param ids
   */
  @Override
  public void deleteStuIds(Integer[] ids) {
    studentMapper.deleteStuIds(ids);
  }

  /**
   * 添加学员 创建时间 修改时间均为本地时间 违纪次数和违纪扣分均为0
   *
   * @param student
   */
  @Override
  public void addInfo(Student student) {
    student.setViolationCount((short) 0);
    student.setViolationScore((short) 0);
    student.setCreateTime(LocalDateTime.now());
    student.setUpdateTime(LocalDateTime.now());
    studentMapper.addInfo(student);
  }

  @Override
  public Student findById(Integer id) {
    return studentMapper.findById(id);
  }

  /**
   * 修改学员
   *
   * @param student
   */
  @Override
  public void update(Student student) {
    // 根据id查找某个id符合的数据进行更新
    student.setUpdateTime(LocalDateTime.now());
    studentMapper.update(student);
  }

  /**
   * 学员违纪处理
   *
   * @param id
   * @param score
   */
  @Override
  public void violation(Integer id, Integer score) {
    //    1.根据id查找是哪个学员
    Student student = studentMapper.findById(id);
    //    2.该学员只要扣分了违纪次数+1，并且违纪扣分累加
    student.setViolationCount((short) (student.getViolationCount() + 1));
    student.setViolationScore((short) (student.getViolationScore() + score));
    studentMapper.update(student);
  }
}
